package com.ftw.commons.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.widget.AppCompatTextView
import com.ftw.commons.R


/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2019/4/19  3:45 PM
 */

@Suppress("DEPRECATED_IDENTITY_EQUALS", "DEPRECATION")
class RTextView(context: Context, attrs: AttributeSet) : AppCompatTextView(context, attrs) {

    //default value
    val ICON_DIR_LEFT = 1
    val ICON_DIR_TOP = 2
    val ICON_DIR_RIGHT = 3
    val ICON_DIR_BOTTOM = 4


    //icon
    private var mIconHeight: Int = 0
    private var mIconWidth: Int = 0
    private var mIconDirection: Int = 0

    //corner
    private var mCornerRadius: Float = 0.toFloat()
    private var mCornerRadiusTopLeft: Float = 0.toFloat()
    private var mCornerRadiusTopRight: Float = 0.toFloat()
    private var mCornerRadiusBottomLeft: Float = 0.toFloat()
    private var mCornerRadiusBottomRight: Float = 0.toFloat()

    //BorderWidth
    private var mBorderDashWidth = 0f
    private var mBorderDashGap = 0f
    private var mBorderWidthNormal = 0
    private var mBorderWidthPressed = 0
    private var mBorderWidthUnable = 0

    //BorderColor
    private var mBorderColorNormal: Int = 0
    private var mBorderColorPressed: Int = 0
    private var mBorderColorUnable: Int = 0

    //Background
    private var mBackgroundColorNormal: Int = 0
    private var mBackgroundColorPressed: Int = 0
    private var mBackgroundColorUnable: Int = 0
    private var mBackgroundNormal: GradientDrawable? = null
    private var mBackgroundPressed: GradientDrawable? = null
    private var mBackgroundUnable: GradientDrawable? = null

    // Text
    private var mTextColorNormal: Int = 0
    private var mTextColorPressed: Int = 0
    private var mTextColorUnable: Int = 0
    private var mTextColorStateList: ColorStateList? = null

    //Icon
    private var mIcon: Drawable? = null
    private var mIconNormal: Drawable? = null
    private var mIconPressed: Drawable? = null
    private var mIconUnable: Drawable? = null

    //typeface
    private var mTypefacePath: String? = null

    private val states = arrayOfNulls<IntArray>(4)
    private var mStateBackground: StateListDrawable? = null
    private val mBorderRadii = FloatArray(8)

    /**
     * Cache the touch slop from the context that created the view.
     */
    private var mTouchSlop: Int = 0
    private var mContext: Context? = null
    private var mGestureDetector: GestureDetector? = null

    /**
     * 是否设置对应的属性
     */
    private var mHasPressedBgColor = false
    private var mHasUnableBgColor = false
    private var mHasPressedBorderColor = false
    private var mHasUnableBorderColor = false
    private var mHasPressedBorderWidth = false
    private var mHasUnableBorderWidth = false

    init {
        mContext = context
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop()
        mGestureDetector = GestureDetector(context, SimpleOnGesture())
        initAttributeSet(context, attrs)
    }


    override fun setEnabled(enabled: Boolean) {
        super.setEnabled(enabled)
        if (enabled) {
            if (mIconNormal != null) {
                mIcon = mIconNormal
                setIcon()
            }
        } else {
            if (mIconUnable != null) {
                mIcon = mIconUnable
                setIcon()
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (!isEnabled) return true
        mGestureDetector!!.onTouchEvent(event)
        when (event.action) {
            MotionEvent.ACTION_UP//抬起
            -> if (mIconNormal != null) {
                mIcon = mIconNormal
                setIcon()
            }
            MotionEvent.ACTION_MOVE//移动
            -> {
                val x = event.x.toInt()
                val y = event.y.toInt()
                if (isOutsideView(x, y)) {
                    if (mIconNormal != null) {
                        mIcon = mIconNormal
                        setIcon()
                    }
                }
            }
            MotionEvent.ACTION_CANCEL//父级控件获取控制权
            -> if (mIconNormal != null) {
                mIcon = mIconNormal
                setIcon()
            }
        }
        return super.onTouchEvent(event)
    }

    /**
     * 初始化控件属性
     *
     * @param context
     * @param attrs
     */
    private fun initAttributeSet(context: Context?, attrs: AttributeSet?) {
        if (context == null || attrs == null) {
            setup()
            return
        }
        val a = context.obtainStyledAttributes(attrs, R.styleable.RTextView)
        //corner
        mCornerRadius = a.getDimensionPixelSize(R.styleable.RTextView_corner_radius, -1).toFloat()
        mCornerRadiusTopLeft = a.getDimensionPixelSize(R.styleable.RTextView_corner_radius_top_left, 0).toFloat()
        mCornerRadiusTopRight = a.getDimensionPixelSize(R.styleable.RTextView_corner_radius_top_right, 0).toFloat()
        mCornerRadiusBottomLeft = a.getDimensionPixelSize(R.styleable.RTextView_corner_radius_bottom_left, 0).toFloat()
        mCornerRadiusBottomRight =
            a.getDimensionPixelSize(R.styleable.RTextView_corner_radius_bottom_right, 0).toFloat()
        //border
        mBorderDashWidth = a.getDimensionPixelSize(R.styleable.RTextView_border_dash_width, 0).toFloat()
        mBorderDashGap = a.getDimensionPixelSize(R.styleable.RTextView_border_dash_gap, 0).toFloat()
        mBorderWidthNormal = a.getDimensionPixelSize(R.styleable.RTextView_border_width_normal, 0)
        mBorderWidthPressed = a.getDimensionPixelSize(R.styleable.RTextView_border_width_pressed, 0)
        mBorderWidthUnable = a.getDimensionPixelSize(R.styleable.RTextView_border_width_unable, 0)
        mBorderColorNormal = a.getColor(R.styleable.RTextView_border_color_normal, Color.TRANSPARENT)
        mBorderColorPressed = a.getColor(R.styleable.RTextView_border_color_pressed, Color.TRANSPARENT)
        mBorderColorUnable = a.getColor(R.styleable.RTextView_border_color_unable, Color.TRANSPARENT)
        //icon
        //Vector兼容处理
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mIconNormal = a.getDrawable(R.styleable.RTextView_icon_src_normal)
            mIconPressed = a.getDrawable(R.styleable.RTextView_icon_src_pressed)
            mIconUnable = a.getDrawable(R.styleable.RTextView_icon_src_unable)
        } else {
            val normalId = a.getResourceId(R.styleable.RTextView_icon_src_normal, -1)
            val pressedId = a.getResourceId(R.styleable.RTextView_icon_src_pressed, -1)
            val unableId = a.getResourceId(R.styleable.RTextView_icon_src_unable, -1)

            if (normalId != -1)
                mIconNormal = AppCompatResources.getDrawable(context, normalId)
            if (pressedId != -1)
                mIconPressed = AppCompatResources.getDrawable(context, pressedId)
            if (unableId != -1)
                mIconUnable = AppCompatResources.getDrawable(context, unableId)
        }
        mIconWidth = a.getDimensionPixelSize(R.styleable.RTextView_icon_width, 0)
        mIconHeight = a.getDimensionPixelSize(R.styleable.RTextView_icon_height, 0)
        mIconDirection = a.getInt(R.styleable.RTextView_icon_direction, ICON_DIR_LEFT)
        //text
        mTextColorNormal = a.getColor(R.styleable.RTextView_text_color_normal, currentTextColor)
        mTextColorPressed = a.getColor(R.styleable.RTextView_text_color_pressed, currentTextColor)
        mTextColorUnable = a.getColor(R.styleable.RTextView_text_color_unable, currentTextColor)
        //background
        mBackgroundColorNormal = a.getColor(R.styleable.RTextView_background_normal, 0)
        mBackgroundColorPressed = a.getColor(R.styleable.RTextView_background_pressed, 0)
        mBackgroundColorUnable = a.getColor(R.styleable.RTextView_background_unable, 0)
        //typeface
        mTypefacePath = a.getString(R.styleable.RTextView_text_typeface)

        a.recycle()

        mHasPressedBgColor = this.mBackgroundColorPressed !== 0
        mHasUnableBgColor = mBackgroundColorUnable !== 0
        mHasPressedBorderColor = mBorderColorPressed !== 0
        mHasUnableBorderColor = mBorderColorUnable !== 0
        mHasPressedBorderWidth = mBorderWidthPressed !== 0
        mHasUnableBorderWidth = mBorderWidthUnable !== 0

        //setup
        setup()

    }

    /**
     * 设置
     */
    private fun setup() {

        mBackgroundNormal = GradientDrawable()
        mBackgroundPressed = GradientDrawable()
        mBackgroundUnable = GradientDrawable()

        val drawable = background
        mStateBackground = if (drawable != null && drawable is StateListDrawable) {
            drawable
        } else {
            StateListDrawable()
        }

        /**
         * 设置背景默认值
         */
        if (!mHasPressedBgColor) {
            mBackgroundColorPressed = mBackgroundColorNormal
        }
        if (!mHasUnableBgColor) {
            mBackgroundColorUnable = mBackgroundColorNormal
        }

        mBackgroundNormal!!.setColor(mBackgroundColorNormal)
        mBackgroundPressed!!.setColor(mBackgroundColorPressed)
        mBackgroundUnable!!.setColor(mBackgroundColorUnable)

        //pressed, focused, normal, unable
        states[0] = intArrayOf(android.R.attr.state_enabled, android.R.attr.state_pressed)
        states[1] = intArrayOf(android.R.attr.state_enabled, android.R.attr.state_focused)
        states[3] = intArrayOf(-android.R.attr.state_enabled)
        states[2] = intArrayOf(android.R.attr.state_enabled)
        mStateBackground!!.addState(states[0], mBackgroundPressed)
        mStateBackground!!.addState(states[1], mBackgroundPressed)
        mStateBackground!!.addState(states[3], mBackgroundUnable)
        mStateBackground!!.addState(states[2], mBackgroundNormal)

        /**
         * icon
         */
        if (!isEnabled) {
            mIcon = mIconUnable
        } else {
            mIcon = mIconNormal
        }

        /**
         * 设置边框默认值
         */
        if (!mHasPressedBorderWidth) {
            mBorderWidthPressed = mBorderWidthNormal
        }
        if (!mHasUnableBorderWidth) {
            mBorderWidthUnable = mBorderWidthNormal
        }
        if (!mHasPressedBorderColor) {
            mBorderColorPressed = mBorderColorNormal
        }
        if (!mHasUnableBorderColor) {
            mBorderColorUnable = mBorderColorNormal
        }

        if (mBackgroundColorNormal === 0 && mBackgroundColorUnable === 0 && mBackgroundColorPressed === 0) {//未设置自定义背景色
            /* if (mBorderColorPressed == 0 && mBorderColorUnable == 0 && mBorderColorNormal == 0) {//未设置自定义边框
                //获取原生背景并设置
                setBackgroundState(true);
            } else {
                setBackgroundState(false);
            }*/
            //获取原生背景并设置
            setBackgroundState(true)
        } else {
            //设置背景资源
            setBackgroundState(false)
        }


        //设置文本颜色
        setTextColor()

        //设置边框
        setBorder()

        //设置ICON
        setIcon()

        //设置圆角
        setRadius()

        //设置文本字体样式
        setTypeface()

    }

    /**
     * 是否移出view
     *
     * @param x
     * @param y
     * @return
     */
    private fun isOutsideView(x: Int, y: Int): Boolean {
        var flag = false
        // Be lenient about moving outside of buttons
        if (x < 0 - mTouchSlop || x >= width + mTouchSlop ||
            y < 0 - mTouchSlop || y >= height + mTouchSlop
        ) {
            // Outside button
            flag = true
        }
        return flag
    }

    /*********************
     * BackgroundColor
     */

    fun setStateBackgroundColor(normal: Int, pressed: Int, unable: Int): RTextView {
        mBackgroundColorNormal = normal
        mBackgroundColorPressed = pressed
        mBackgroundColorUnable = unable
        mHasPressedBgColor = true
        mHasUnableBgColor = true
        mBackgroundNormal!!.setColor(mBackgroundColorNormal)
        mBackgroundPressed!!.setColor(mBackgroundColorPressed)
        mBackgroundUnable!!.setColor(mBackgroundColorUnable)
        setBackgroundState(false)
        return this
    }

    fun getBackgroundColorNormal(): Int {
        return mBackgroundColorNormal
    }

    fun setBackgroundColorNormal(colorNormal: Int): RTextView {
        this.mBackgroundColorNormal = colorNormal
        /**
         * 设置背景默认值
         */
        if (!mHasPressedBgColor) {
            mBackgroundColorPressed = mBackgroundColorNormal
            mBackgroundPressed!!.setColor(mBackgroundColorPressed)
        }
        if (!mHasUnableBgColor) {
            mBackgroundColorUnable = mBackgroundColorNormal
            mBackgroundUnable!!.setColor(mBackgroundColorUnable)
        }
        mBackgroundNormal!!.setColor(mBackgroundColorNormal)
        setBackgroundState(false)
        return this
    }

    fun getBackgroundColorPressed(): Int {
        return mBackgroundColorPressed
    }

    fun setBackgroundColorPressed(colorPressed: Int): RTextView {
        this.mBackgroundColorPressed = colorPressed
        this.mHasPressedBgColor = true
        mBackgroundPressed!!.setColor(mBackgroundColorPressed)
        setBackgroundState(false)
        return this
    }

    fun getBackgroundColorUnable(): Int {
        return mBackgroundColorUnable
    }

    fun setBackgroundColorUnable(colorUnable: Int): RTextView {
        this.mBackgroundColorUnable = colorUnable
        this.mHasUnableBgColor = true
        mBackgroundUnable!!.setColor(mBackgroundColorUnable)
        setBackgroundState(false)
        return this
    }

    @SuppressLint("ObsoleteSdkInt")
    private fun setBackgroundState(unset: Boolean) {

        //未设置自定义属性,并且设置背景颜色时
        val drawable = background
        if (unset && drawable is ColorDrawable) {
            val color = drawable.color
            setStateBackgroundColor(color, color, color)//获取背景颜色值设置 StateListDrawable
        }

        //设置背景资源
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            setBackgroundDrawable(if (unset) drawable else mStateBackground)
        } else {
            background = if (unset) drawable else mStateBackground
        }
    }


    /************************
     * Typeface
     */

    fun setTypeface(typefacePath: String): RTextView {
        this.mTypefacePath = typefacePath
        setTypeface()
        return this
    }

    fun getTypefacePath(): String {
        return mTypefacePath!!
    }

    private fun setTypeface() {
        if (!TextUtils.isEmpty(mTypefacePath)) {
            val assetManager = mContext!!.getAssets()
            val typeface = Typeface.createFromAsset(assetManager, mTypefacePath)
            setTypeface(typeface)
        }
    }

    /************************
     * Icon
     */

    fun setIconNormal(icon: Drawable): RTextView {
        this.mIconNormal = icon
        this.mIcon = icon
        setIcon()
        return this
    }

    fun getIconNormal(): Drawable {
        return mIconNormal!!
    }

    fun setIconPressed(icon: Drawable): RTextView {
        this.mIconPressed = icon
        this.mIcon = icon
        setIcon()
        return this
    }

    fun getIconPressed(): Drawable {
        return mIconPressed!!
    }

    fun setIconUnable(icon: Drawable): RTextView {
        this.mIconUnable = icon
        this.mIcon = icon
        setIcon()
        return this
    }

    fun getIconUnable(): Drawable {
        return mIconUnable!!
    }

    fun setIconSize(iconWidth: Int, iconHeight: Int): RTextView {
        this.mIconWidth = iconWidth
        this.mIconHeight = iconHeight
        setIcon()
        return this
    }

    fun setIconWidth(iconWidth: Int): RTextView {
        this.mIconWidth = iconWidth
        setIcon()
        return this
    }

    fun getIconWidth(): Int {
        return mIconWidth
    }

    fun setIconHeight(iconHeight: Int): RTextView {
        this.mIconHeight = iconHeight
        setIcon()
        return this
    }

    fun getIconHeight(): Int {
        return mIconHeight
    }

    fun setIconDirection(iconDirection: Int): RTextView {
        this.mIconDirection = iconDirection
        setIcon()
        return this
    }

    fun getIconDirection(): Int {
        return mIconDirection
    }

    private fun setIcon() {
        //未设置图片大小
        if (mIconHeight === 0 && mIconWidth === 0) {
            if (mIcon != null) {
                mIconWidth = mIcon!!.getIntrinsicWidth()
                mIconHeight = mIcon!!.getIntrinsicHeight()
            }
        }
        setIcon(mIcon, mIconWidth, mIconHeight, mIconDirection)
    }

    private fun setIcon(drawable: Drawable?, width: Int, height: Int, direction: Int) {
        if (drawable != null) {
            if (width != 0 && height != 0) {
                drawable.setBounds(0, 0, width, height)
            }
            when (direction) {
                ICON_DIR_LEFT -> setCompoundDrawables(drawable, null, null, null)
                ICON_DIR_TOP -> setCompoundDrawables(null, drawable, null, null)
                ICON_DIR_RIGHT -> setCompoundDrawables(null, null, drawable, null)
                ICON_DIR_BOTTOM -> setCompoundDrawables(null, null, null, drawable)
            }
        }
    }

    /************************
     * text color
     */

    fun setTextColorNormal(textColor: Int): RTextView {
        this.mTextColorNormal = textColor
        if (mTextColorPressed === 0) {
            mTextColorPressed = mTextColorNormal
        }
        if (mTextColorUnable === 0) {
            mTextColorUnable = mTextColorNormal
        }
        setTextColor()
        return this
    }

    fun getTextColorNormal(): Int {
        return mTextColorNormal
    }

    fun setPressedTextColor(textColor: Int): RTextView {
        this.mTextColorPressed = textColor
        setTextColor()
        return this
    }

    fun getPressedTextColor(): Int {
        return mTextColorPressed
    }

    fun setTextColorUnable(textColor: Int): RTextView {
        this.mTextColorUnable = textColor
        setTextColor()
        return this
    }

    fun getTextColorUnable(): Int {
        return mTextColorUnable
    }

    fun setTextColor(normal: Int, pressed: Int, unable: Int) {
        this.mTextColorNormal = normal
        this.mTextColorPressed = pressed
        this.mTextColorUnable = unable
        setTextColor()
    }

    private fun setTextColor() {
        val colors = intArrayOf(mTextColorPressed, mTextColorPressed, mTextColorNormal, mTextColorUnable)
        mTextColorStateList = ColorStateList(states, colors)
        setTextColor(mTextColorStateList)
    }

    /*********************
     * border
     */

    fun setBorderWidthNormal(width: Int): RTextView {
        this.mBorderWidthNormal = width
        if (!mHasPressedBorderWidth) {
            mBorderWidthPressed = mBorderWidthNormal
            setBorderPressed()
        }
        if (!mHasUnableBorderWidth) {
            mBorderWidthUnable = mBorderWidthNormal
            setBorderUnable()
        }
        setBorderNormal()
        return this
    }

    fun getBorderWidthNormal(): Int {
        return mBorderWidthNormal
    }

    fun setBorderColorNormal(color: Int): RTextView {
        this.mBorderColorNormal = color
        if (!mHasPressedBorderColor) {
            mBorderColorPressed = mBorderColorNormal
            setBorderPressed()
        }
        if (!mHasUnableBorderColor) {
            mBorderColorUnable = mBorderColorNormal
            setBorderUnable()
        }
        setBorderNormal()
        return this
    }

    fun getBorderColorNormal(): Int {
        return mBorderColorNormal
    }

    fun setBorderWidthPressed(width: Int): RTextView {
        this.mBorderWidthPressed = width
        this.mHasPressedBorderWidth = true
        setBorderPressed()
        return this
    }

    fun getBorderWidthPressed(): Int {
        return mBorderWidthPressed
    }

    fun setBorderColorPressed(color: Int): RTextView {
        this.mBorderColorPressed = color
        this.mHasPressedBorderColor = true
        setBorderPressed()
        return this
    }

    fun getBorderColorPressed(): Int {
        return mBorderColorPressed
    }

    fun setBorderWidthUnable(width: Int): RTextView {
        this.mBorderWidthUnable = width
        this.mHasUnableBorderWidth = true
        setBorderUnable()
        return this
    }

    fun getBorderWidthUnable(): Int {
        return mBorderWidthUnable
    }

    fun setBorderColorUnable(color: Int): RTextView {
        this.mBorderColorUnable = color
        this.mHasUnableBorderColor = true
        setBorderUnable()
        return this
    }

    fun getBorderColorUnable(): Int {
        return mBorderColorUnable
    }

    fun setBorderWidth(normal: Int, pressed: Int, unable: Int) {
        this.mBorderWidthNormal = normal
        this.mBorderWidthPressed = pressed
        this.mBorderWidthUnable = unable
        this.mHasPressedBorderWidth = true
        this.mHasUnableBorderWidth = true
        setBorder()
    }

    fun setBorderColor(normal: Int, pressed: Int, unable: Int) {
        this.mBorderColorNormal = normal
        this.mBorderColorPressed = pressed
        this.mBorderColorUnable = unable
        this.mHasPressedBorderColor = true
        this.mHasUnableBorderColor = true
        setBorder()
    }

    fun setBorderDashWidth(dashWidth: Float) {
        this.mBorderDashWidth = dashWidth
        setBorder()
    }

    fun getBorderDashWidth(): Float {
        return mBorderDashWidth
    }

    fun setBorderDashGap(dashGap: Float) {
        this.mBorderDashGap = dashGap
        setBorder()
    }

    fun getBorderDashGap(): Float {
        return mBorderDashGap
    }

    fun setBorderDash(dashWidth: Float, dashGap: Float) {
        this.mBorderDashWidth = dashWidth
        this.mBorderDashGap = dashGap
        setBorder()
    }

    private fun setBorder() {
        mBackgroundNormal!!.setStroke(mBorderWidthNormal, mBorderColorNormal, mBorderDashWidth, mBorderDashGap)
        mBackgroundPressed!!.setStroke(mBorderWidthPressed, mBorderColorPressed, mBorderDashWidth, mBorderDashGap)
        mBackgroundUnable!!.setStroke(mBorderWidthUnable, mBorderColorUnable, mBorderDashWidth, mBorderDashGap)
        setBackgroundState(false)
    }

    private fun setBorderNormal() {
        mBackgroundNormal!!.setStroke(mBorderWidthNormal, mBorderColorNormal, mBorderDashWidth, mBorderDashGap)
        setBackgroundState(false)
    }

    private fun setBorderPressed() {
        mBackgroundPressed!!.setStroke(mBorderWidthPressed, mBorderColorPressed, mBorderDashWidth, mBorderDashGap)
        setBackgroundState(false)
    }

    private fun setBorderUnable() {
        mBackgroundUnable!!.setStroke(mBorderWidthUnable, mBorderColorUnable, mBorderDashWidth, mBorderDashGap)
        setBackgroundState(false)
    }


    /*********************
     * radius
     */

    fun setCornerRadius(radius: Float) {
        this.mCornerRadius = radius
        setRadius()
    }

    fun getCornerRadius(): Float {
        return mCornerRadius
    }

    fun setCornerRadiusTopLeft(topLeft: Float): RTextView {
        this.mCornerRadius = (-1).toFloat()
        this.mCornerRadiusTopLeft = topLeft
        setRadius()
        return this
    }

    fun getCornerRadiusTopLeft(): Float {
        return mCornerRadiusTopLeft
    }

    fun setCornerRadiusTopRight(topRight: Float): RTextView {
        this.mCornerRadius = (-1).toFloat()
        this.mCornerRadiusTopRight = topRight
        setRadius()
        return this
    }

    fun getCornerRadiusTopRight(): Float {
        return mCornerRadiusTopRight
    }

    fun setCornerRadiusBottomRight(bottomRight: Float): RTextView {
        this.mCornerRadius = (-1).toFloat()
        this.mCornerRadiusBottomRight = bottomRight
        setRadius()
        return this
    }

    fun getCornerRadiusBottomRight(): Float {
        return mCornerRadiusBottomRight
    }

    fun setCornerRadiusBottomLeft(bottomLeft: Float): RTextView {
        this.mCornerRadius = -1.toFloat()
        this.mCornerRadiusBottomLeft = bottomLeft
        setRadius()
        return this
    }

    fun getCornerRadiusBottomLeft(): Float {
        return mCornerRadiusBottomLeft
    }

    fun setCornerRadius(topLeft: Float, topRight: Float, bottomRight: Float, bottomLeft: Float) {
        this.mCornerRadius = -1.toFloat()
        this.mCornerRadiusTopLeft = topLeft
        this.mCornerRadiusTopRight = topRight
        this.mCornerRadiusBottomRight = bottomRight
        this.mCornerRadiusBottomLeft = bottomLeft
        setRadius()
    }

    private fun setRadiusRadii() {
        mBackgroundNormal!!.setCornerRadii(mBorderRadii)
        mBackgroundPressed!!.setCornerRadii(mBorderRadii)
        mBackgroundUnable!!.setCornerRadii(mBorderRadii)
        setBackgroundState(false)
    }

    private fun setRadius() {
        if (mCornerRadius >= 0) {
            mBorderRadii[0] = mCornerRadius
            mBorderRadii[1] = mCornerRadius
            mBorderRadii[2] = mCornerRadius
            mBorderRadii[3] = mCornerRadius
            mBorderRadii[4] = mCornerRadius
            mBorderRadii[5] = mCornerRadius
            mBorderRadii[6] = mCornerRadius
            mBorderRadii[7] = mCornerRadius
            setRadiusRadii()
            return
        }

        if (mCornerRadius < 0) {
            mBorderRadii[0] = mCornerRadiusTopLeft
            mBorderRadii[1] = mCornerRadiusTopLeft
            mBorderRadii[2] = mCornerRadiusTopRight
            mBorderRadii[3] = mCornerRadiusTopRight
            mBorderRadii[4] = mCornerRadiusBottomRight
            mBorderRadii[5] = mCornerRadiusBottomRight
            mBorderRadii[6] = mCornerRadiusBottomLeft
            mBorderRadii[7] = mCornerRadiusBottomLeft
            setRadiusRadii()
            return
        }
    }

    private fun dp2px(dp: Int): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), resources.displayMetrics)
    }

    /**
     * 手势处理
     */
    internal inner class SimpleOnGesture : GestureDetector.SimpleOnGestureListener() {
        override fun onShowPress(e: MotionEvent) {
            if (mIconPressed != null) {
                mIcon = mIconPressed
                setIcon()
            }
        }

        override fun onSingleTapUp(e: MotionEvent): Boolean {
            if (mIconNormal != null) {
                mIcon = mIconNormal
                setIcon()
            }
            return false
        }
    }


}


